package be.bonamis.umpire.business.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import be.bonamis.umpire.business.UmpireService;
import be.bonamis.umpire.domain.Address;
import be.bonamis.umpire.domain.Club;
import be.bonamis.umpire.domain.GameType;
import be.bonamis.umpire.domain.Ranking;
import be.bonamis.umpire.domain.Umpire;
import be.bonamis.umpire.old.domain.CsvDispoArbitres;
import be.bonamis.umpire.old.domain.XlsCaUmpire;
import be.bonamis.umpire.old.domain.XlsRanking;
import be.bonamis.umpire.old.utils.ImportData;
import be.bonamis.umpire.repository.ClubRepository;
import be.bonamis.umpire.repository.GameTypeRepository;
import be.bonamis.umpire.repository.RankingRepository;
import be.bonamis.umpire.repository.UmpireRepository;
import be.bonamis.umpire.utils.FileUtils;
import be.bonamis.umpire.utils.Mappings;

@Service
public class UmpireServiceImpl implements UmpireService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(UmpireServiceImpl.class);

	@Autowired
	private RankingRepository rankingDao;

	@Autowired
	private ClubRepository clubDao;

	@Autowired
	private GameTypeRepository gameDao;

	@Autowired
	private UmpireRepository umpireDao;

	private void importRankings(String fileName) {
		List<XlsRanking> list = FileUtils.read(fileName);
		for (XlsRanking xlsRanking : list) {
			Ranking ranking = new Ranking();
			ranking.setName(xlsRanking.getRanking());
			rankingDao.save(ranking);
		}
	}

	private void importClubs(String fileName) {
		List<Club> list = FileUtils.read(fileName);
		for (Club clubItem : list) {
			Club club = new Club();
			club.setCode(clubItem.getCode());
			club.setDimaSportCode(clubItem.getDimaSportCode());
			club.setName(clubItem.getName());
			club.setNumber(clubItem.getNumber());
			clubDao.save(club);
		}
	}

	private void importGameTypes(String fileName) {
		List<GameType> list = FileUtils.read(fileName);
		for (GameType gameType : list) {
			gameDao.save(gameType);
		}
	}

	@Override
	public void mergeOldDatabase(String arbitreCAFile, String dispoArbitreFile,
			String clubFile, String trainingFile, String gameTypeFile,
			String rankingFile, String beepTestFile) {

		List<CsvDispoArbitres> oldDbList = ImportData
				.getOldDbUmpiresList(dispoArbitreFile);

		List<XlsCaUmpire> list = FileUtils.read(arbitreCAFile);
		importClubs(clubFile);
		importGameTypes(gameTypeFile);
		importRankings(rankingFile);

		for (CsvDispoArbitres dispoArbitres : oldDbList) {
			Umpire umpire = Mappings.Umpires.fromOldDbtoNew
					.apply(dispoArbitres);

			XlsCaUmpire xlsCa = ImportData.getXlsCaUmpirebyId(list,
					dispoArbitres.getId());

			if (xlsCa != null) {
				umpire.setFirstName(xlsCa.getFirstName());
				umpire.setLastName(xlsCa.getName());

				Address adr = new Address();
				adr.setHouseNumber(xlsCa.getNbr());
				adr.setMunicipality(xlsCa.getCity());
				adr.setPostalCode(xlsCa.getZipCode());
				adr.setStreet(xlsCa.getStreet());
				umpire.setAddress(adr);

				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
				try {
					umpire.setBirthDay(sdf.parse(xlsCa.getBirthdate()));
				} catch (ParseException e) {
					LOGGER.error("ParseException with the Birthdate: ");
				}

				umpire.setClub(clubDao.findByName(xlsCa.getClubName()));
				umpire.setEmail(xlsCa.getEmail());
				umpire.setMemberId(Integer.valueOf(xlsCa.getMemberID()));
				// umpire.setRanking()

				umpireDao.save(umpire);
			} else {
				LOGGER.debug(dispoArbitres.getNomArbitre() + " not found");
				System.out
						.println(dispoArbitres.getNomArbitre() + " not found");
			}
		}
	}

	@Override
	public Umpire getUmpireByLoginAndPassword(String login, String password) {
		Umpire umpire = umpireDao.findByLogin(login);
		if (umpire.getPassword().equals(DigestUtils.md5Hex(password))) {
			return umpire;
		}
		return null;
	}
}
